lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'laydate'], {
    data: {
        $layui: null,
        tableins: null,
        search: {
            sdate: null,
            edate: null,
            keyword: "",
            status: 0
        },
    },
    pageload: function (right) {
        console.log(right);
        this.data.search.sdate = this.getStartTime();
        this.data.search.edate = this.getEndTime();
        this.loadData();
        this.initevent();
    },
    initevent() {
        var self = this;
        // 订单查询开始时间
        layui.laydate.render({
            elem: "#starttime",
            //type: "datetime",
            format: 'yyyy-MM-dd',
            type: "date",
            //format: 'yyyy-MM-dd',
            value: self.data.search.sdate + ' ~ ' + self.data.search.edate,
            range: '~',
            change: function (value, date, endDate) {
                $("#starttime").val(value);
            },
            done: function (value, date, endDate) {
                $("#starttime").val(value);
            }
        });

        layui.form.on('submit(LAY-list-submit)', data => {
            var dstr = $("#starttime").val();
            self.data.search.sdate = dstr.split(" ~ ")[0];
            self.data.search.edate = dstr.split(" ~ ")[1];
            self.data.search.status = $("#RegState").val();
            self.data.search.keyword = $("#key").val();
            self.loadData();
        });

        layui.table.on("tool(datatable)", function (obj) {
            console.log(obj)
            if (obj.event == "view") {
                window.currData = obj.data;
                layer.open({
                    type: 2,
                    title: obj.data.PatName + "挂号详情",
                    btn: ['关闭'],
                    btnAlign: "r",
                    content: "registerdetail.html",
                    area: ['600px', '80%'],
                    maxmin: true,
                    success: function (layero, index) {
                    }
                });
            }
        });
    },
    loadData() {
        var self = this;
        self.data.registertable = layui.table.render({
            elem: '#datatable',
            where: self.data.search,
            headers: lobsterlayui.GetHeaderData(),
            url: lobsterlayui.GetLowCodeUrl('tsregister.getregrecordlist_web'),
            height: 'full-130',
            cellMinWidth: 80,
            size: 'sm',
            toolbar: '#toolbar',
            page: true,
            even: true,
            cols: [[
                { width: "100", field: "Id", title: "编号", align: 'center' },
                { field: 'PatName', title: '就诊人姓名', width: "120", },
                // { field: 'BranchHospitalName', title: '分院名称', width: "160", },
                { field: 'DeptName', title: '科室', width: "160" },
                { field: 'EmpName', title: '医生', width: "160" },
                { field: 'RegDate', title: '预约时间', align: 'left', width: "200", templet: (d) => { return d.RegDate + " " + d.RegRange; } },
                { field: 'RegState', title: '挂号状态', width: "160", templet: "#RegStateTemp" },
                { field: 'CreateTime', title: '下单时间', align: 'left', width: "160", templet: (d) => { return d.CreateTime.substr(0, 19); } },
                { field: 'PayMode', title: '支付方式', width: "160", templet: "#ModelStateTemp" },                
                { field: 'OrderNo', title: '订单号', align: 'left', width: "200" },
                { field: 'TranserNo', title: '业务号', align: 'left', width: "200" },

                { field: '', title: '操作', fixed: "right", align: 'left', width: "120", toolbar: '#viewoption' }
            ]],
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.list
                }
            }
        });
    },
    getEndTime: function () {
        var mdate = new Date();
        var year = mdate.getFullYear();
        var month = mdate.getMonth() + 1;
        if (month < 10)
            month = "0" + month;
        var ddate = mdate.getDate();
        if (ddate < 10)
            ddate = "0" + ddate;
        return year + "-" + month + "-" + ddate;
    },
    getStartTime: function () {
        var mdate = new Date();
        var year = mdate.getFullYear();
        var month = mdate.getMonth() + 1;
        if (month < 10)
            month = "0" + month;
        var ddate = mdate.getDate() - 3;
        if (ddate < 10)
            ddate = "0" + ddate;
        return year + "-" + month + "-" + ddate;
    }
})